package com.spdd.mt.task;

import com.spdd.mt.biz.config.ConfigBean;
import com.spdd.mt.biz.manager.feishu.FeiShuService;
import com.spdd.mt.biz.manager.service.BatchService;
import com.spdd.mt.config.ScheduledCondition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * 每日生成对账单任务
 *
 * @since 2025年04月06日
 */
@Slf4j
@Component
@Conditional(ScheduledCondition.class)
public class GenerateCheckFileTask {

    @Autowired
    private ConfigBean configBean;

    @Autowired
    private BatchService batchService;

    @Autowired
    private FeiShuService feiShuService;

    /**
     * 每天晚上0点10分生成对账文件
     */
    @Scheduled(cron = "0 10 0 * * ?")
    public void createFile() {
        LocalDate lastDay = LocalDate.now().minusDays(1);
        log.info("开始执行生成对账文件任务，日期:{}", lastDay.toString());
        try {
            boolean flag = batchService.createFile(lastDay.toString().replace("-", ""));
            if (flag) {
                log.info("生成{}对账文件成功", lastDay.toString());
            } else {
                log.info("生成{}对账文件失败", lastDay.toString());
            }
        } catch (Exception e) {
            feiShuService.sendExceptionMsg(e);
            log.error("生成对账文件异常", e);
        }
        log.info("生成{}的对账文件任务结束", lastDay.toString());
    }
}
